package com.ninong.ker.admin.rest;

import com.ninong.ker.admin.domain.FwFormConfig;
import com.ninong.ker.admin.service.dto.FwFormConfigDTO;
import com.ninong.ker.admin.sevice.FwFormConfigService;
import com.ninong.ker.common.annotation.perm.CmdAdmin;
import com.ninong.ker.common.response.CmdResponse;
import com.ninong.ker.common.tools.PropertyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * 表单配置
 *
 * @author 鸠摩智
 * @email cmd
 * @date 2021-02-07 17:00:45
 */
@RestController
@RequestMapping("/cmd/v1/formconfig")
public class FwFormConfigResources {

    @Autowired
    private FwFormConfigService fwFormConfigService;

    /**
     * 列表
     */
    @CmdAdmin({"admin", "form:list"})
    @PostMapping
    public ResponseEntity<CmdResponse> list(@RequestBody FwFormConfigDTO dto) {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.listPage(dto));
        return ResponseEntity.ok(response);
    }

    @CmdAdmin({"admin", "form:delete"})
    @DeleteMapping("/{formCode}/{language}")
    public ResponseEntity<CmdResponse> delete(@RequestBody String[] ids) {
        fwFormConfigService.delete(ids);
        CmdResponse response = new CmdResponse();
        String mess = PropertyUtil.getProperty("DELETE_SUCCESS");
        response.setMessage(mess);
        return ResponseEntity.ok(response);
    }

    @CmdAdmin({"admin", "form:add", "form:update"})
    @PutMapping
    public ResponseEntity<CmdResponse> create(@RequestBody FwFormConfig entity) throws Exception {
        fwFormConfigService.create(entity);
        return ResponseEntity.ok(new CmdResponse());
    }
    
    @GetMapping("/{formCode}/{language}")
    public ResponseEntity<CmdResponse> get(@PathVariable String formCode,@PathVariable String language) {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.get(formCode,language));
        return ResponseEntity.ok(response);
    }

    @GetMapping("/{id}")
    public ResponseEntity<CmdResponse> get(@PathVariable String id) {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.get(id));
        return ResponseEntity.ok(response);
    }

    @GetMapping("/feild/{formCode}/{language}")
    public ResponseEntity<CmdResponse> getFeild(@PathVariable String formCode,@PathVariable String language) {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.getFeild(formCode,language));
        return ResponseEntity.ok(response);
    }

    @GetMapping("/condition/{formCode}/{language}")
    public ResponseEntity<CmdResponse> getFeildCondition(@PathVariable String formCode,@PathVariable String language) {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.getFeildCondition(formCode,language));
        return ResponseEntity.ok(response);
    }

    /**
     * 表单集合
     * @return
     */
    @GetMapping("/treeForm/{confId}")
    public ResponseEntity<CmdResponse> treeForm(@PathVariable String confId) {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.treeForm(confId));
        return ResponseEntity.ok(response);
    }

    /**
     * 查询农事模板list
     * @param type
     * @param tag
     * @return
     */
    @GetMapping(value = "/findList/{type}/{tag}")
    public ResponseEntity findList(@PathVariable String type,@PathVariable String tag){
        CmdResponse response = new CmdResponse();
        FwFormConfigDTO dto = new FwFormConfigDTO();
        dto.setType(type);
        if(StringUtils.isNotEmpty(tag) && !"undefined".equals(tag)) {
            dto.setTag(tag);
        }
        response.setData(fwFormConfigService.findListByDto(dto));
        return ResponseEntity.ok(response);
    }

    /**
     * 查询模板list
     * @param dto
     * @return
     */
    @GetMapping(value = "/findFormList")
    public ResponseEntity findFormList(FwFormConfigDTO dto){
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.findListByDto(dto));
        return ResponseEntity.ok(response);
    }

    @GetMapping("/feilds/{formId}/{language}")
    public ResponseEntity<CmdResponse> getFeilds(@PathVariable String formId,@PathVariable String language) {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.getFeilds(formId,language));
        return ResponseEntity.ok(response);
    }

    @GetMapping("/columns/{formId}/{language}")
    public ResponseEntity<CmdResponse> getTableColumns(@PathVariable String formId,@PathVariable String language) {
        CmdResponse response = new CmdResponse();
        response.setData(fwFormConfigService.getTableColumns(formId,language));
        return ResponseEntity.ok(response);
    }
}
